﻿Imports System.Data.SqlClient
Public Class frmCatalogoTanques
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoTanques_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo de Tanques")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmCatalogoTanques_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgBombas.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al catálogo de Tanques")
        Usuario.Ubicacion("Catálogo de Tanques")

        txtBuscar.Clear()
        CargaTanques()
        txtBuscar.Focus()
        dgBombas.Columns(1).ReadOnly = False
        dgBombas.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgBombas.Columns(5).DefaultCellStyle.Format = "##,##0.0"
        dgBombas.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgBombas.Columns(4).DefaultCellStyle.Format = "##,##0.0"
    End Sub
    Sub CargaTanques()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgBombas.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select *" & _
                     "from Tanques " & _
                     "WHERE UPPER(Nombre) LIKE '" & Filtro & "' " & mstrCondicion & _
                     " OR UPPER(Ubicacion) LIKE '" & Filtro & "' " & mstrCondicion


            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim Altura As Double
            Dim Longitud As Double
            Dim Diametro As Double
            Dim Radio As Double
            Dim AlfaRadianes As Double
            Dim AlfaGrados As Double
            Dim Volumen As Double
            Dim Heliptica As Double
            Dim VolumenH As Double
            Dim VolumenTotal As Double
            Dim Litros As Double
            Dim Existencia As Double


            Do While rdBuscar.Read()
                'Genera Capacidad
                Longitud = rdBuscar("Longitud")
                Diametro = rdBuscar("Diametro")
                Radio = Diametro / 2
                AlfaRadianes = Math.Acos(1 - (Diametro / Radio))
                AlfaGrados = (AlfaRadianes * 180) / 3.1416
                Volumen = Longitud * (Radio * Radio) * (AlfaGrados / 57.3 - (Math.Sin(AlfaRadianes) * Math.Cos(AlfaRadianes)))
                Heliptica = rdBuscar("AltoGlobo")
                If Heliptica > 0 Then
                    VolumenH = 3.1416 * (Diametro * Diametro) * Heliptica / 6
                    VolumenTotal = VolumenH * 2 + Volumen
                    Litros = VolumenTotal * 1000
                Else
                    VolumenTotal = Volumen
                    Litros = VolumenTotal * 1000
                End If

                'Genera Existencia
                'Genera Capacidad
                Altura = rdBuscar("Medida_Actual")
                Longitud = rdBuscar("Longitud")
                Diametro = rdBuscar("Diametro")
                Radio = Diametro / 2
                AlfaRadianes = Math.Acos(1 - (Altura / Radio))
                AlfaGrados = (AlfaRadianes * 180) / 3.1416
                Volumen = Longitud * (Radio * Radio) * (AlfaGrados / 57.3 - (Math.Sin(AlfaRadianes) * Math.Cos(AlfaRadianes)))
                Heliptica = rdBuscar("AltoGlobo")
                If Heliptica > 0 Then
                    VolumenH = 3.1416 * (Diametro * Diametro) * Heliptica / 6
                    VolumenTotal = VolumenH * 2 + Volumen
                    Existencia = VolumenTotal * 1000
                Else
                    VolumenTotal = Volumen
                    Existencia = VolumenTotal * 1000
                End If

                dgBombas.Rows.Add(rdBuscar("IdTanque"), False, rdBuscar("Nombre").ToString.Trim, rdBuscar("Ubicacion").ToString.Trim, Litros, Existencia)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub Explorar()
        Dim valorCell As Integer
        Dim valorRow As DataGridViewRow

        valorCell = dgBombas.CurrentCell.ColumnIndex
        valorRow = dgBombas.CurrentRow

        TanqueSelect = dgBombas.Rows(valorRow.Index).Cells(2).Value.ToString.Trim
        TanqueNuevo = False

        frmCatalogoTanquesDetalle.ShowDialog()
    End Sub
    Sub ImprimeFiltro()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook


        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim rng As Integer = 5

        For i = 2 To 5
            wb.SHEETS(1).cells(rng, i).FONT.name = "Segoe UI Light"
            wb.SHEETS(1).cells(rng, i).font.bold = True
            wb.SHEETS(1).CELLS(rng, i).HorizontalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).VerticalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).WrapText = True
            wb.SHEETS(1).CELLS(rng, i).Orientation = 0
            wb.SHEETS(1).CELLS(rng, i).AddIndent = False
            wb.SHEETS(1).CELLS(rng, i).IndentLevel = 0
            wb.SHEETS(1).CELLS(rng, i).ShrinkToFit = False
            wb.SHEETS(1).CELLS(rng, i).MergeCells = False
            wb.SHEETS(1).CELLS(rng, i).ReadingOrder = -5002
        Next
        wb.SHEETS(1).cells(rng, 2).value = "Nombre"
        wb.SHEETS(1).cells(rng, 3).value = "Ubicacion"
        wb.SHEETS(1).cells(rng, 4).value = "Capacidad"
        wb.SHEETS(1).cells(rng, 5).value = "Existencia"
        wb.SHEETS(1).columns(2).columnwidth = 30
        wb.SHEETS(1).columns(3).columnwidth = 30
        wb.SHEETS(1).columns(4).columnwidth = 12
        wb.SHEETS(1).columns(4).NumberFormat = "#,##0.0"
        wb.SHEETS(1).columns(5).columnwidth = 12
        wb.SHEETS(1).columns(5).NumberFormat = "#,##0.0"

        rng = rng + 1
        For i = 0 To dgBombas.Rows.Count - 1
            For j = 2 To 5
                wb.SHEETS(1).cells(rng, j).FONT.name = "Segoe UI Light"

            Next
            wb.SHEETS(1).cells(rng, 2).value = dgBombas.Rows(i).Cells(2).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 3).value = dgBombas.Rows(i).Cells(3).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 4).value = dgBombas.Rows(i).Cells(4).Value
            wb.SHEETS(1).cells(rng, 5).value = dgBombas.Rows(i).Cells(5).Value.ToString.Trim
            rng = rng + 1
        Next

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Vista previa del Catálogo de Tanques"
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de Tanques")
        ImprimeFiltro()
    End Sub

    Private Sub ExportarFiltroAExcelToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportarFiltroAExcelToolStripMenuItem.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de Tanques")
        ImprimeFiltro()
    End Sub

    Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click
        Explorar()
    End Sub

    Private Sub ExaminarRegistroToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExaminarRegistroToolStripMenuItem.Click
        Explorar()
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()
    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub ToolStripButton5_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton5.Click
        TanqueNuevo = True
        frmCatalogoTanquesDetalle.ShowDialog()
    End Sub

    Private Sub NuevaToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NuevaToolStripMenuItem.Click
        TanqueNuevo = True
        frmCatalogoTanquesDetalle.ShowDialog()
    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaTanques()
    End Sub
    Private Sub dgBombas_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgBombas.DoubleClick
        Explorar()
    End Sub

    Private Sub dgBombas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgBombas.KeyPress
        Explorar()
    End Sub

    Private Sub dgBombas_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgBombas.CellContentClick

    End Sub
End Class